Imports System.ComponentModel
Imports System.Web.UI


<Designer(GetType(BookInfoDesigner)), ParseChildren(False), ControlBuilder(GetType(BookBuilder)), _
ToolboxData("<{0}:BookData runat=server></{0}:BookData>")> _
 Public Class BookData
    Inherits System.Web.UI.WebControls.WebControl
    Private _ISBN As String
    Private _MainTitle As String
    Private _SubTitle As String
    Private _Authors As ArrayList
    Private _Description As String
    Private _ChildControls As ArrayList

    'Public Property ChildControls() As ArrayList
    '    Get
    '        Return _ChildControls
    '    End Get
    '    Set(ByVal value As ArrayList)
    '        _ChildControls = value
    '    End Set
    'End Property

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property MainTitle() As String
        Get
            Return _MainTitle
        End Get
        Set(ByVal value As String)
            _MainTitle = value
        End Set
    End Property

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(True), PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property SubTitle() As String
        Get
            Return _SubTitle
        End Get
        Set(ByVal value As String)
            _SubTitle = value
        End Set
    End Property

    Public Property Authors() As ArrayList
        Get
            Return _Authors
        End Get
        Set(ByVal value As ArrayList)
            _Authors = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property

    Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)

        If obj.GetType.Equals(GetType(BookInfo.Description)) Then
            Me.Description = CType(obj, BookInfo.Description).Text
        End If

        If obj.GetType.Equals(GetType(Author)) Then
            If _Authors Is Nothing Then
                _Authors = New ArrayList(2)
            End If
            Me.Authors.Add(obj)
        End If

        If obj.GetType.Equals(GetType(Titles)) Then
            Me.MainTitle = CType(obj, Titles).MainTitle
            Me.SubTitle = CType(obj, Titles).SubTitle
        End If

        If obj.GetType.Equals(GetType(System.Web.UI.WebControls.Label)) Then
            Me.Controls.Add(obj)
        End If

    End Sub

    Public Property ISBN() As String
        Get
            Return _ISBN
        End Get
        Set(ByVal value As String)
            _ISBN = value
        End Set
    End Property

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        '    writer.Write("<Table>")
        '    For Each aut As Author In Me.Authors
        '        writer.Write("<tr>")
        '        writer.Write("<tc>" & aut.FirstName & "</tc>")
        '        writer.Write("<tc>" & aut.LastName & "</tc>")
        '        writer.Write("</tr>")
        '    Next
        '    writer.Write("</Table>")
        '    writer.Write(Me.MainTitle)
        '    writer.Write(Me.SubTitle)
        writer.Write(Me.Description)
        '    writer.Write(Me.Controls.Count)

        '    For Each obj As Object In Me.Authors
        '        Select Case obj.GetType.Name
        '                    if "Label"
        '                End Select
        '        writer.Write(aut.LastName)
        '    Next
        '    For Each ctl As Control In Me.Controls
        '        ctl.RenderControl(writer)
        '    Next

        '    writer.Write(CType(Me.Authors.Item(0), Author).LastName)
        '    writer.Write("<Table>")
        '    For Each ttl As Title In Me.Titles
        '        writer.Write("<tr>")
        '        writer.Write("<tc>" & ttl.Main & "</tc>")
        '        writer.Write("<tc>" & ttl.SubTitle & "</tc>")
        '        writer.Write("</tr>")
        '    Next
        '    writer.Write("</Table>")
        '    For Each ctl As Control In Me.ChildControls
        '        ctl.RenderControl(writer)
        '    Next
    End Sub

    'Private Sub BookData_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    '    If Me.DesignMode = True Then
    '        _Authors.Add("Peter Vogel")
    '        _Authors.Add("Sara Shlaer")
    '        _Authors.Add("Richard Purchase")
    '    End If
    'End Sub
End Class

Public Class Author
    Private _FirstName As String
    Private _LastName As String

    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

End Class

Public Class Titles
    Private _MainT As String
    Private _SubT As String

    Public Property MainTitle() As String
        Get
            Return _MainT
        End Get
        Set(ByVal value As String)
            _MainT = value
        End Set
    End Property

    Public Property SubTitle() As String
        Get
            Return _SubT
        End Get
        Set(ByVal value As String)
            _SubT = value
        End Set
    End Property

End Class

Public Class BookBuilder
    Inherits System.Web.UI.ControlBuilder

    Public Overrides Function AllowWhitespaceLiterals() As Boolean
        Return True
    End Function
    
    Public Overrides Function GetChildControlType(ByVal tagName As String, ByVal attribs As System.Collections.IDictionary) As System.Type

        Select Case tagName
            Case "Author"
                Return GetType(Author)
            Case "Titles"
                Return GetType(Titles)
            Case "Description"
                Return GetType(Description)
            Case "asp:Label"

            Case Else
                Throw New Exception(tagName & " not supported.")
        End Select
    End Function

    Public Overrides Function NeedsTagInnerText() As Boolean
        Return True
    End Function

    Public Overrides Sub AppendSubBuilder(ByVal subBuilder As System.Web.UI.ControlBuilder)

        If subBuilder.AllowWhitespaceLiterals = False Then
            Throw New Exception("Whitespace must be supported.")
        Else
            MyBase.AppendSubBuilder(subBuilder)
        End If

    End Sub

    Public Overrides Sub OnAppendToParentBuilder(ByVal parentBuilder As System.Web.UI.ControlBuilder)

        If ControlType.Name = "BookInfo" Then
            If Me.FChildrenAsProperties = True Then
                Throw New Exception("This builder should not be used with ParseChildren.")
            End If
        End If
    End Sub
End Class


